package cn.kgc.crowdfundingadmin03.secrity;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-03-06 9:18
 */
@Configuration   //配置类
@EnableWebSecurity  //启动secrity功能
@EnableGlobalMethodSecurity(prePostEnabled = true)//启用全局方法权限管理功能
public class MySecutityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //授权

        //开启表单登陆功能

        http.authorizeRequests()//对请求进行授权
                .antMatchers("/static/**", "/index")//使用ant风格设置需要授权的地址
                .permitAll()//允许上面使用ant风格设置的全部请求

//                .antMatchers("/admin/query/for/search")
//                .hasAnyAuthority("user:get")

                .anyRequest()//其他未设置的全部需要请求
                .authenticated()//需要认证

                .and()
                .formLogin()//设置未授权请求调转到登陆页面
                .loginPage("/admin/to/login/page.html")//指定跳转到登陆页面
                .permitAll()//为登陆页面设置所有人都可以访问

                //登陆
                .loginProcessingUrl("/admin/secutity/do/login")//指定登陆地址不适用原来的控制器
                .usernameParameter("loginAcct")//指定参数
                .passwordParameter("userPswd")
                .defaultSuccessUrl("/admin/to/main/page.html")//登陆成功页面
                .failureUrl("/admin/to/login/page.html?error=true")//登陆失败跳转并传参进行判断

                //启动退出功能
                .and()
                .logout()
                .logoutUrl("/admin/security/logout")
                .logoutSuccessUrl("/index")


                .and()
                .csrf()
                .disable() // 禁用CSRF功能，如果不禁用，只能发post请求退出，否则报错
        ;

        http.rememberMe().rememberMeParameter("remember");

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //认证

//        auth.inMemoryAuthentication()
//                .passwordEncoder(new BCryptPasswordEncoder())
//                .withUser("test")
//                .password(new BCryptPasswordEncoder().encode("123456"))
//                .roles("任海鹏");
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());

    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
